package com.walker.kuyun.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.ViewStub
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.walker.kuyun.R
import com.walker.kuyun.databinding.FragmentTmShowBinding
import com.walker.kuyun.ui.adapter.TMShowAdapter
import com.walker.kuyun.ui.adapter.SerializableRecycledViewPool
import com.walker.kuyun.util.Util.showToast
import com.walker.kuyun.viewmodel.TMShowViewModel
import java.io.Serializable


class TMShowFragment : BaseFragment<FragmentTmShowBinding>() {
    private var partUrl: String = ""
    private lateinit var viewModel: TMShowViewModel
    private lateinit var TMShowAdapter: TMShowAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel = ViewModelProvider(this).get(TMShowViewModel::class.java)
        val arguments = arguments

        try {
            partUrl = arguments?.getString("partUrl") ?: ""
            viewModel.viewPool =
                arguments?.getSerializable("viewPool") as SerializableRecycledViewPool
            viewModel.childViewPool =
                arguments.getSerializable("childViewPool") as SerializableRecycledViewPool
        } catch (e: Exception) {
            e.printStackTrace()
            e.message?.showToast(Toast.LENGTH_LONG)
        }
    }

    override fun getBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentTmShowBinding = FragmentTmShowBinding.inflate(inflater, container, false)

    override fun onResume() {
        super.onResume()
        if (isFirstLoadData) {
            initData()
            isFirstLoadData = false
        }
    }

    private fun initData() {
        val childViewPool = viewModel.childViewPool
        TMShowAdapter =
            TMShowAdapter(
                this,
                viewModel.videoShowList,
                childViewPool ?: RecyclerView.RecycledViewPool()
            )

        mBinding.run {
            showLayout.apply {
                layoutManager = LinearLayoutManager(activity)
                setHasFixedSize(true)
                adapter = TMShowAdapter
                viewModel.viewPool?.let {
                    setRecycledViewPool(it)
                }
            }
            showRefreshLayout.apply {
                setOnRefreshListener {
                    viewModel.getAnimeShowData(partUrl)
                }
                setOnLoadMoreListener {
                    viewModel.pageNumberBean?.let {
                        viewModel.getAnimeShowData(it.actionUrl, isRefresh = false)
                        return@setOnLoadMoreListener
                    }
                    finishLoadMore()
                    getString(R.string.no_more_info).showToast()
                }
            }
        }

        viewModel.mldGetAnimeShowList.observe(viewLifecycleOwner, {
            mBinding.showRefreshLayout.closeHeaderOrFooter()
            when (it) {
                0 -> {
                    TMShowAdapter.notifyDataSetChanged()
                    hideLoadFailedTip()
                }
                1 -> {
                    val pair = viewModel.newPageIndex
                    if (pair != null) {
                        TMShowAdapter.notifyItemRangeInserted(pair.first, pair.second)
                    } else TMShowAdapter.notifyDataSetChanged()
                    hideLoadFailedTip()
                }
                -1 -> {
                    TMShowAdapter.notifyDataSetChanged()
                    showLoadFailedTip(
                        getString(R.string.load_data_failed_click_to_retry)
                    ) {
                        viewModel.getAnimeShowData(partUrl)
                        hideLoadFailedTip()
                    }
                }
            }
        })
        refresh()
    }

    fun refresh(): Boolean {
        return mBinding.showRefreshLayout.autoRefresh()
    }

    override fun getLoadFailedTipView(): ViewStub = mBinding.layoutAnimeShowFragmentLoadFailed

    companion object {
        const val TAG = "AnimeShowFragment"
        fun newInstance(url:String,serialViewPool: Serializable,serialChildViewPool:Serializable): TMShowFragment {
            val fragment = TMShowFragment()
            val bundle = Bundle()
            bundle.putString("partUrl", url)
            bundle.putSerializable("viewPool", serialViewPool)
            bundle.putSerializable("childViewPool", serialChildViewPool)
            fragment.arguments = bundle
            return fragment
        }
    }
}